﻿using System;
using System.Text;

namespace Douya.Json
{
    public class JsonString : IJsonValue
    {
        string value;

        public JsonString(string value)
        {
            this.value = value;
        }

        public ValueType Type
        {
            get { return ValueType.TypeString; }
        }

        public bool GetBoolean()
        {
            return !String.IsNullOrWhiteSpace(value);
        }

        public int GetInteger()
        {
            return int.Parse(value);
        }

        public string GetString()
        {
            return value;
        }

        public JsonObject GetObject()
        {
            return null;
        }

        public JsonArray GetArray()
        {
            return null;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            //return "\"" + value + "\"";
            builder.Append('"');
            foreach (char chr in value)
            {
                if (chr == '\"')
                {
                    builder.Append("\\\"");
                }
                else if (chr == '\\')
                {
                    builder.Append("\\\\");
                }
                else if (chr == '\n')
                {
                    builder.Append("\\n");
                }
                else if (chr == '\r')
                {
                    builder.Append("\\r");
                }
                else if (chr == '\t')
                {
                    builder.Append("\\t");
                }
                else if (chr == '\f')
                {
                    builder.Append("\\f");
                }
                else if (chr == '\b')
                {
                    builder.Append("\\b");
                }
                else
                {
                    builder.Append(chr);
                }
            }
            builder.Append('"');
            return builder.ToString();
        }
    }
}
